package org.jeecg.modules.wangyue.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.jeecg.modules.wangyue.entity.ServiceItems;
import org.jeecg.modules.wangyue.entity.ServiceType;
import org.jeecg.modules.wangyue.mapper.ServiceItemsMapper;
import org.jeecg.modules.wangyue.mapper.ServiceTypeMapper;
import org.jeecg.modules.wangyue.model.req.ReqCommonModel;
import org.jeecg.modules.wangyue.service.IServiceTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

/**
 * @Description: 服务类型表
 * @Author: jeecg-boot
 * @Date:   2023-06-30
 * @Version: V1.0
 */
@Service
public class ServiceTypeServiceImpl extends ServiceImpl<ServiceTypeMapper, ServiceType> implements IServiceTypeService {

    @Autowired
    private ServiceTypeMapper serviceTypeMapper;
    @Autowired
    private ServiceItemsMapper serviceItemsMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean switchShow(ServiceType st) {
        try{
            /*先改变该服务类型的所有服务套餐逻辑删除标识，再改变该服务类型逻辑删除标识*/
            ServiceType serviceType = serviceTypeMapper.selectById(st.getId());

            UpdateWrapper<ServiceItems> serviceItemsUpdateWrapper = new UpdateWrapper<>();
            serviceItemsUpdateWrapper.eq("service_type_code", serviceType.getServiceTypeCode());
            serviceItemsUpdateWrapper.set("del_flag", st.getDelFlag());
            serviceItemsMapper.update(null, serviceItemsUpdateWrapper);

            UpdateWrapper<ServiceType> serviceTypeUpdateWrapper = new UpdateWrapper<>();
            serviceTypeUpdateWrapper.eq("id", serviceType.getId());
            serviceTypeUpdateWrapper.set("del_flag", st.getDelFlag());
            serviceTypeMapper.update(null, serviceTypeUpdateWrapper);

            return true;
        }catch (Exception e){
//            e.printStackTrace();
            throw new RuntimeException("服务类型开关操作失败！");
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteServiceType(ReqCommonModel reqCommonModel) {
        try{
            /*先删除该服务类型的服务套餐数据，再删除该服务类型数据*/
            ServiceType serviceType = serviceTypeMapper.selectById(reqCommonModel.getId());
            QueryWrapper<ServiceItems> serviceItemsQueryWrapper = new QueryWrapper<>();
            serviceItemsQueryWrapper.eq("service_type_code",serviceType.getServiceTypeCode());
            serviceItemsMapper.delete(serviceItemsQueryWrapper);
            serviceTypeMapper.deleteById(serviceType.getId());

            return true;
        }catch (Exception e){
//            e.printStackTrace();
            throw new RuntimeException("服务类型删除操作失败！");
        }
    }


}
